package com.credit.mola.app.utils.dialog

import android.content.Context
import android.os.Build
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import com.credit.mola.lib_permission.OnPermissionCallback
import com.credit.mola.lib_permission.XXPermissions
import com.credit.mola.app.R
import com.credit.mola.app.app.App
import com.credit.mola.app.databinding.DialogPhotoBinding
import com.credit.mola.app.pro.ProDialog
import com.credit.mola.app.ui.permission.PermHelper
import com.credit.mola.app.utils.event.EventType
import com.credit.mola.app.utils.http.HttpHelper
import org.json.JSONObject

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 拍照、相册选择 dialog
 */
class PhotoDialog: ProDialog {
    private lateinit var binding: DialogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""
    //是否是相机
    private var isCamera = true
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun initWindow() {
        val params = window?.attributes
        params?.width = App.app.getPhoneWidth()
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    override fun getRootView(): View {
        binding = DialogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        isCamera = true
        onCheckType()
    }

    override fun initListener() {
        setOnClickListener(R.id.btnConfirm) //确定
        binding.cameraLayout.setOnClickListener {
            isCamera = true
            onCheckType()
        }

        binding.albumLayout.setOnClickListener {
            isCamera = false
            onCheckType()
        }
    }

    override fun initData() {
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.btnConfirm -> {
                if (isCamera){//相机
                    val item = PermHelper.getCreditMolaCameraItem()
                    if (XXPermissions.isGranted(context,item.optString("permission"))){
                        mCallback?.openCamera()
                    } else {
                        showCreditMolaPermissionReasonDialog(item)
                    }
                } else {
                    mCallback?.openAlbum()
                }
            }
        }
    }

    /**
     * 切换
     */
    private fun onCheckType(){
        binding.cameraLayout.setBackgroundResource(R.drawable.radius_fill_fff0f0f0_12)
        binding.imgCamera.setImageResource(R.mipmap.img_ocr_camera_normal)
        binding.textCamera.setTextColor(ContextCompat.getColor(context,R.color.FF6D6D6D))
        binding.imgCameraSelect.setImageResource(R.mipmap.img_ocr_photo_normal)

        binding.albumLayout.setBackgroundResource(R.drawable.radius_fill_fff0f0f0_12)
        binding.imgAlbum.setImageResource(R.mipmap.img_ocr_album_normal)
        binding.textAlbum.setTextColor(ContextCompat.getColor(context,R.color.FF6D6D6D))
        binding.imgAlbumSelect.setImageResource(R.mipmap.img_ocr_photo_normal)

        if (isCamera){
            binding.cameraLayout.setBackgroundResource(R.drawable.ocr_photo_select)
            binding.imgCamera.setImageResource(R.mipmap.img_ocr_camera_select)
            binding.textCamera.setTextColor(ContextCompat.getColor(context,R.color.FF4C8FFB))
            binding.imgCameraSelect.setImageResource(R.mipmap.img_ocr_photo_select)
        } else {
            binding.albumLayout.setBackgroundResource(R.drawable.ocr_photo_select)
            binding.imgAlbum.setImageResource(R.mipmap.img_ocr_album_select)
            binding.textAlbum.setTextColor(ContextCompat.getColor(context,R.color.FF4C8FFB))
            binding.imgAlbumSelect.setImageResource(R.mipmap.img_ocr_photo_select)
        }
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    /**
     * 显示权限说明
     */
    private fun showCreditMolaPermissionReasonDialog(item: JSONObject){
        val requestPermission = item.optString("permission")
        val dialog = PermissionReasonDialog(context,requestPermission,object : PermissionReasonDialog.IDialogCallback{
            override fun onPermissionAgreeClick() {
                HttpHelper.addEvent(EventType.CLICK.CLICK_PERMISSION_REASON_DIALOG_AGREE,requestPermission)
                requestCreditMolaPermission(item)
            }

            override fun onPermissionDenyClick() {
                HttpHelper.addEvent(EventType.CLICK.CLICK_PERMISSION_REASON_DIALOG_DENY,requestPermission)
            }
        })
        dialog.setCancelable(true)
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()
        dialog.showSikadwaPermissionReason(item)
    }

    /**
     * 请求权限
     */
    private fun requestCreditMolaPermission(item: JSONObject){
        val requestPermission = item.optString("permission")
        XXPermissions.with(context)
            // 申请多个权限
            .permission(requestPermission)
            .request(object:
                OnPermissionCallback {
                override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                    var permissionString = ""
                    p0.forEach {
                        permissionString = "$permissionString$it,"
                    }
                    HttpHelper.addEvent(EventType.CLICK.CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                    mCallback?.openCamera()
                }
                override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
                    var permissionString = ""
                    p0.forEach {
                        permissionString = "$permissionString$it,"
                    }
                    HttpHelper.addEvent(EventType.CLICK.CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
                    XXPermissions.startPermissionActivity(context,requestPermission)
                }
            })
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PHOTO_DISMISS,eventRemark)
    }
}